package cn.fuxi2022.module.pay.Listener;

import cn.fuxi2022.module.pay.service.WeChatPayService;
import cn.fuxi2022.rabbitmq.appletsPay.constants.WeChatPayConstants;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 订单消息消费者
 * 注意：可以使用 多个线程并发或JUC，这里不演示
 */
@Slf4j
@Component
public class WeChatPayConsumer {

    @Autowired
    private WeChatPayService weChatPayService;

    /**
     * 处理订单超时队列 监听接口
     */
    @RabbitListener(queues = WeChatPayConstants.GROUP_ORDER_DLE_ORDER_QUEUE)
    public void orderDleQueueListener(String data, Channel channel, Message message) throws IOException {
        log.info("订单超时关闭{}", data);
        weChatPayService.closeOrder(data);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    /**
     * 处理释放库存队列 监听接口
     */
    @RabbitListener(queues = WeChatPayConstants.GROUP_ORDER_DLE_ALERT_QUEUE)
    public void alertDleQueueListener(String data, Channel channel, Message message) throws IOException {
        log.info("订单消息提醒{}", data);
        weChatPayService.sendOrderMsg(data);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
